//接受一个对象为参数
//
const myAxios = function (config) {
    return new Promise((resolve, reject) => {
        console.log(config);
        const { method, url, data, params } = config
        //1.
        const xhr = new XMLHttpRequest()
        //2.
        if (method.toUpperCase() === 'GET') {
            xhr.open('GET', url)
            xhr.send()
        } else {
            xhr.open('POST', url)
            let flag = Object.prototype.toString.call(data)==='[object Object]'
            xhr.setRequestHeader('Content-Type', flag ? 'application/json' : 'application/x-www-form-urlencoded')
            xhr.send(flag ? JSON.stringify(data) : data)
        }
        //4.
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    resolve(JSON.parse(xhr.status))
                }
                else {
                    reject('请求失败' + xhr.status)
                }
            }
        }
    })
}
function fn(obj) {  
    let arr = []
    for (let key in obj) {
        let str = k + '=' + obj[k]
        arr.push(str)
    }
    return arr.join('&')
}